import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

# 生成或加载密钥
SECRET_KEY = b"your-secret-key-here-1234567890"
kdf = PBKDF2HMAC(
    algorithm=hashes.SHA256(),
    length=32,
    salt=b"your-salt-here-123",
    iterations=100000,
)
key = base64.urlsafe_b64encode(kdf.derive(SECRET_KEY))
fernet = Fernet(key)

def encrypt_token(data: str) -> str:
    """加密数据生成token"""
    return fernet.encrypt(data.encode()).decode()

def decrypt_token(token: str) -> str:
    """解密token获取数据"""
    try:
        return fernet.decrypt(token.encode()).decode()
    except:
        return None

def verify_login_token(token: str) -> bool:
    """验证token是否为有效登录信息"""
    decrypted = decrypt_token(token)
    if decrypted:
        username, password = decrypted.split(":")
        return username == "13800138000" and password == "123456"
    return False